//
// Decompiled by Jadx - 402ms
//
package fj;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public final class d {
    public static void a(Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (IOException unused) {
        }
    }

    public static File[] b(String str) {
        File[] listFiles;
        ArrayList arrayList = new ArrayList();
        File file = new File(str);
        if (!file.isDirectory() || (listFiles = file.listFiles((FileFilter) new b())) == null) {
            return new File[0];
        }
        String[] strArr = new String[0];
        for (File file2 : listFiles) {
            File absoluteFile = file2.getAbsoluteFile();
            if (!Arrays.deepToString(strArr).contains(absoluteFile.getName())) {
                arrayList.add(absoluteFile);
            }
        }
        arrayList.sort(new a());
        return (File[]) arrayList.toArray(new File[arrayList.size()]);
    }

    public static File[] c(String[] strArr, String str) {
        File[] listFiles;
        File[] listFiles2;
        File[] b = b(str);
        if (strArr == null) {
            ArrayList arrayList = new ArrayList();
            File file = new File(str);
            if (!file.isDirectory() || (listFiles2 = file.listFiles((FileFilter) new c())) == null) {
                listFiles = new File[0];
            } else {
                for (File file2 : listFiles2) {
                    arrayList.add(file2.getAbsoluteFile());
                }
                Collections.sort(arrayList, new a());
                listFiles = (File[]) arrayList.toArray(new File[arrayList.size()]);
            }
        } else {
            listFiles = new File(str).listFiles((FilenameFilter) new a(strArr));
        }
        if (listFiles == null) {
            return null;
        }
        ArrayList arrayList2 = new ArrayList(Arrays.asList(b));
        for (File file3 : listFiles) {
            if (!arrayList2.contains(file3)) {
                arrayList2.add(file3);
            }
        }
        return (File[]) arrayList2.toArray(new File[0]);
    }

    public static byte[] d(String str) {
        FileInputStream fileInputStream;
        new File(str).setReadable(true, false);
        FileInputStream fileInputStream2 = null;
        try {
            fileInputStream = new FileInputStream(str);
        } catch (IOException unused) {
            fileInputStream = null;
        } catch (Throwable th) {
            th = th;
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[1024];
            while (true) {
                int read = fileInputStream.read(bArr, 0, 1024);
                if (read == -1) {
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    byteArrayOutputStream.close();
                    a(fileInputStream);
                    return byteArray;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
        } catch (IOException unused2) {
            a(fileInputStream);
            return null;
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            a(fileInputStream2);
            throw th;
        }
    }

    public static String e(String str) {
        try {
            byte[] d = d(str);
            if (d != null) {
                return new String(d, "utf-8").trim();
            }
        } catch (UnsupportedEncodingException unused) {
        }
        return "";
    }

    public static boolean f(InputStream inputStream, String str) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(str));
            byte[] bArr = new byte[1024];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    fileOutputStream.close();
                    inputStream.close();
                    return true;
                }
                fileOutputStream.write(bArr, 0, read);
            }
        } catch (IOException unused) {
            return false;
        }
    }

    public static boolean g(String str, String str2) {
        try {
            byte[] bytes = str2.getBytes("utf-8");
            File file = new File(str);
            FileOutputStream fileOutputStream = null;
            try {
                if (!file.exists()) {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                FileOutputStream fileOutputStream2 = new FileOutputStream(str);
                try {
                    fileOutputStream2.write(bytes);
                    a(fileOutputStream2);
                } catch (IOException unused) {
                    fileOutputStream = fileOutputStream2;
                    a(fileOutputStream);
                    return true;
                } catch (Throwable th) {
                    th = th;
                    fileOutputStream = fileOutputStream2;
                    a(fileOutputStream);
                    throw th;
                }
            } catch (IOException unused2) {
            } catch (Throwable th2) {
                th = th2;
            }
            return true;
        } catch (UnsupportedEncodingException unused3) {
            return false;
        }
    }
}
